<?php

/**
 * 按照表名获取字段
 */
namespace ServS;

use think\Db;
class DbShowcollo{
	/**
	 * 查看数据库的表结构的字段列
	 * @param  [type] $quo [description]
	 * @return [type]      [description]
	 * 使用  DbShowcollo::getcoll($model -> gettablename())
	 */
	public static function getcoll($quo){
	 	$slie  = Db::query("SHOW COLUMNS FROM `$quo`");
	 	$field = [];
	 	foreach ($slie as $key => $value) {
	 		if($value['Field']=='addtime')continue;
	 		if($value['Field']=='add_time')continue;
	 		array_push($field, $value['Field']);
	 	}
	 	return $field;
	}

	/**
	 * 更新字段
	 * DbShowcollo::refresh(模型，要更新的字段，更新的字段值，那些更新)
	 */
	public static function refresh($model,$fied='id',$fiedcn,$array){
		$rest = $model::where($fied,$fiedcn)->update($array);
		return $rest;
	}

	/**
	 * 单条件查询
	 * 用模型单个的条件查询所有的数据
	 */
	public static function getselbysigfield($model,$fied,$fiedcn){
		$res = 	$model::where($fied,$fiedcn)->select();
		$aphs = [];
		foreach ($res as $key => $value) {
			array_push($aphs, $value->toArray());
		}
		return $aphs;
	}

	/**
	 * 模型保存数据字段
	 * @param [type] $model [模型]
	 * @param [type] $field [模型的字段]
	 * @param [type] $array [description]
	 */
	public static function add($model,$array){
		$tablename=$model->gettablename($model);
		$field = self::getcoll($tablename);
		foreach ($field as $key => $value) {
			if($field[$key]=='id')continue;
			if(array_key_exists($value,$array)){
				$model->$field[$key]=$array[$value];
			}
			else{
				$model->$field[$key]='';
			}	
		}
		$model->addtime=time();
		if($model->save()){
			return $model->id;
		}
		return 2;
	}

	/**
	 * 统一删除
	 */
	public static function dele($model,$id){
		if($modeldele = $model::destroy($id)){
			return 1;
		}
		return 0;
	}

}
